package com.zxy.ziems.server.file.util;

import com.zxy.ziems.server.file.enums.MineTypeEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function;

/**
 * @author chdzq
 * @create 2023/3/21
 */
public class FileUtil {
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
    public static Function<String, String> getFileNameByPath = (path) -> {
        String name = (new File(path.trim())).getName();
        return name.substring(0, name.lastIndexOf("."));
    };
    public static Function<String, String> getSuffixByPath = (path) -> {
        String name = (new File(path.trim())).getName();
        return name.substring(name.lastIndexOf(".") + 1, name.length());
    };
    public static Function<String, String> getParentPath = (path) -> {
        return (new File(path.trim())).getParent().concat(File.separator);
    };
    public static Function<String, String> getTmpFilePath = (path) -> {
        return path.substring(0, path.indexOf(".") + 1).concat("tmp");
    };
    public static Function<String, String> getMineType = (path) -> {
        InputStream is = null;

        String var4;
        try {
            File file;
            try {
                byte[] bytes = new byte[28];
                file = new File(path);
                if (!file.exists()) {
                    var4 = null;
                    return var4;
                }

                is = new FileInputStream(file);
                is.read(bytes, 0, 28);
                var4 = MineTypeEnum.getKeys.apply(bytesToHexString(bytes).toUpperCase());
            } catch (Exception var15) {
                logger.error("GetFileHeader Failed : Cause ->{}, Message ->{}", var15.getCause(), var15.getMessage());
                return "";
            }
        } finally {
            try {
                is.close();
            } catch (IOException var14) {
                logger.error("Close IO Stream Failed : Cause ->{}, Message ->{}", var14.getCause(), var14.getMessage());
                return "";
            }
        }

        return var4;
    };

    public FileUtil() {
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src != null && src.length > 0) {
            for(int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv);
            }

            return stringBuilder.toString();
        } else {
            return null;
        }
    }

    public static String md5(MultipartFile file) throws IOException {
        byte[] uploadBytes = file.getBytes();
        //file->byte[],生成md5
        return DigestUtils.md5Hex(uploadBytes);
    }
}

